<?php

namespace app\common\tool;

use app\system\model\Attachment;

class Upload
{
    /**
     * @title   文件上传
     * @desc    方法描述
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   file file &nbsp; 字段名(图片png,jpg视频mp4,支持批量上传) YES
     * @return  int code 200/100 返回参数
     * @return  string msg ok 返回信息
     * @return  array data &nbsp; 返回数据
     * @return  array other &nbsp; 返回数据
     */
    public static function file($files, $paths = false)
    {
        if (empty($files)) {
            return tocode(100, '请传入文件或图片！');
        }
        $y_path = true;
        $data = [];
        $other = [];
        foreach ($files as $file) {
            // $fileName = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . date('YmdHis')) . '.' . $file->getOriginalExtension();
            $fileName = str_replace('.' . $file->getOriginalExtension(), '', createNoncestr(5) . date('YmdHis')) . '.' . $file->getOriginalExtension();
            if (empty($paths)) {
                $paths = explode('/', $file->getMime())[0];
                $y_path = false;
            }
            $md5 = $file->hash('md5');
            switch (config_group('config_images_setup')) {
                case '1':
                    $path = 'http://' . config('filesystem.disks.qiniu.domain') . '/';
                    $savename = \think\facade\Filesystem::disk('qiniu')->putFileAs($paths, $file, $fileName);
                    break;
                case '2':
                    $path = 'http://' . config('filesystem.disks.aliyun.domain') . '/';
                    $savename = \think\facade\Filesystem::disk('aliyun')->putFileAs($paths, $file, $fileName);
                    break;
                case '3':
                    $path = 'http://' . config('filesystem.disks.qcloud.domain') . '/';
                    $savename = \think\facade\Filesystem::disk('qcloud')->putFileAs($paths, $file, $fileName);
                    break;
                default:
                    $savename = \think\facade\Filesystem::disk('public')->putFile($paths, $file);
                    break;
            }
            $arrpath = str_replace('\\', '/', $savename);
            if ($savename) {
                $file_id = createNoncestr();
                // $data['url'] = request()->domain() . '/upload/' . $arrpath;
                $data['id'] = $file_id;
                $data['name'] = $file->getOriginalName();
                $data['size'] = $file->getSize();
                $data['type'] = $file->getMime();
                if ($y_path) {
                    rename(root_path('public') . 'upload/' . $arrpath, root_path() . $paths . '/' . $file->getOriginalName());
                } else {
                    $add['url'] = $arrpath;
                    $add['name'] = $file->getOriginalName();
                    $add['storage'] = config_group('config_images_setup');
                    $add['filesize'] = $file->getSize();
                    $add['mimetype'] = $file->getMime();
                    $add['sha1'] = $file->hash('sha1');
                    $add['storage'] = config_group('config_images_setup');
                    $add['md5'] = $file->hash('md5');
                    $add['file_id'] = $file_id;
                    // $add['create_time'] = time();
                    // $add['update_time'] = time();
                    if (!Attachment::where('file_id', $file_id)->count()) {
                        Attachment::create($add);
                    }
                }
                $data['url'] = _sImage($file_id);
            } else {
                $other[] = $file->getError();
            }
        }
        $res = [
            'data' => $data,
            'other' => $other,
        ];
        $file_data = Attachment::field('file_id,storage,url')->select();
        $attachment = [];
        foreach ($file_data as $k => $v) {
            $attachment[$v['file_id']] = [
                'storage' => $v['storage'],
                'url' => $v['url'],
            ];
        }
        cache('attachment', $attachment);
        return $res;
    }
    /**
     * 上传文件到图床
     */
    public static function sm_ms($file)
    {
        $url = "https://sm.ms/api/v2/upload";
        $headers = [
            'Authorization: XOkwCOQyuOgFKBVdYm7SWTOO0Uzwz50T',
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36',
        ];
        $data = ['smfile' => new \CURLFile($file)];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $a = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($a, true);
        return $res;
    }
}
